package com.puman.watchtrade.fragment.sell.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.puman.watchtrade.MainActivity;
import com.puman.watchtrade.R;
import com.puman.watchtrade.fragment.sell.SellFragment;
import com.puman.watchtrade.fragment.sell.model.SellImgs;
import com.puman.watchtrade.fragment.sell.util.CheckImageLoaderConfiguration;
import com.puman.watchtrade.fragment.sell.util.UniversalImageLoadTool;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class GridViewAdapter extends BaseAdapter {
	Context context;
	private SellImgs sellImgs = new SellImgs();
	private SellFragment sellFragment;

	public GridViewAdapter(SellFragment sellFragment,Context _context, SellImgs sellImgs) {
		this.sellFragment = sellFragment;
		this.sellImgs = sellImgs;
		this.context = _context;
	}

	@Override
	public int getCount() {
		return sellImgs.sellimgList.size();
	}

	@Override
	public Object getItem(int position) {
		return sellImgs.sellimgList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater layoutInflater = LayoutInflater.from(context);
		convertView = layoutInflater.inflate(R.layout.list_item, null);
		// se city = sellImgs.sellimgList.get(position);
		ImageView imageView = (ImageView) convertView
				.findViewById(R.id.ItemImage);
		ImageView delete = (ImageView) convertView
				.findViewById(R.id.delete);
		// 如果有头像，载入，否则显示layout上面的（@drawable/home_new_icon）
		CheckImageLoaderConfiguration
				.checkImageLoaderConfiguration(context.getApplicationContext());
//		UniversalImageLoadTool.clear();
		UniversalImageLoadTool.display(sellImgs.sellimgList.get(position)
				.getImageUrl(), (ImageView) imageView, R.drawable.icon);
        
		if (sellImgs.sellimgList.get(position).isSelect) {
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
				imageView.setBackground(MainActivity.getInstant().getResources().getDrawable(R.drawable.iwantsale_img_red_bg));
			}else {
				imageView.setBackgroundDrawable(MainActivity.getInstant().getResources().getDrawable(R.drawable.iwantsale_img_red_bg));
			}
//			imageView.setBackground(MainActivity.getInstant().getResources().getDrawable(R.drawable.iwantsale_img_red_bg));
			delete.setVisibility(View.VISIBLE);
			delete.setTag(position);
			delete.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					int posi = (Integer) v.getTag();
					sellFragment.sellImgs.sellimgList.remove(posi);
					notifyDataSetChanged();
					Log.i("curry", "delete:");
				}
			});
		}else {
			imageView.setBackgroundColor((MainActivity.getInstant().getResources().getColor(R.color.transparent)));
			delete.setVisibility(View.INVISIBLE);
			delete.setOnClickListener(null);
		}
		
		return convertView;
	}
}
